const Colors = require('colors')
const path = require('path')
const fs = require('fs')

function errorLog(message, data) {
  console.log(`${Colors.red(message)}`, data || '')
}

function warnLog(message, data) {
  console.log(`${Colors.yellow(message)}`, data || '')
}

function infoLog(message, data) {
  console.log(`${Colors.green(message)}`, data || '')
}

/**
 * @description: 获取项目路径地址
 * @return {String} 路径地址
*/
function getProjectPath(dirname) {
  return path.join(__dirname, '../', dirname || '')
}

/**
 * @description: 退出进程
 * @return {void}
*/
function exit() {
  process.exit(1)
}

/**
 * @description: 同步读取文件
 * @return {String} 读取的文件数据
*/
function readFileSync(path) {
  infoLog(`${path} 读取中...`)
  let data = fs.readFileSync(path, { encoding: 'utf-8' })
  infoLog(`${path} 读取成功...`)
  
  return data
}

/**
 * @description: 同步写入文件
 * @return {void}
*/
function writeFileSync(path, data) {
  infoLog(`${path} 写入中...`)
  fs.writeFileSync(path, data)
  infoLog(`${path} 写入成功...`)
}

module.exports = {
  errorLog,
  warnLog,
  infoLog,
  getProjectPath,
  exit,
  readFileSync,
  writeFileSync
}
